// pages/customer/customer_details.js
const myaudio = wx.createInnerAudioContext();
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow:false,  //资料点击更多是否展示更多
    analysis_isShow:true,  //客户分析点击更多是否展示更多
    look_more:"查看更多",
    analysis_more:"收起",
    id:'',//id
    tabId:"",//历程tab切换id.默认最早
    financial:"",//全部记录0 只看财务1
    remarks:"",//备注
    financialShow:false,
    audioArr: [
      {
        id: '000',
        src: 'http://mp3.djwma.com/mp3/爆袭全站欢快节奏感觉那是杠杠滴.mp3',
        bl: false
      },
      {
        id: '001',
        src: 'http://mp3.djwma.com/mp3/好听的欧美男声 网络流行.mp3',
        bl: false
      }],
    user_detail:[],
    progress:[],
    code:'', //导航类型
    roleCode:"",//角色身份
    studyRecord:"",//学习记录
    vipRecord:"",//vip记录
    empty_title:"暂无数据",
    empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
    empty_again:"",//重试
    weekOrHistory:"历史排名",
    analysis_detail:"",//重点分析数据
    analysis_type:2,//重点分析类型  1周 2历史
    isAnalysis:false, //是否显示重要度分析介绍弹窗
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var that = this;

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      code:options.code,
      roleCode:options.roleCode
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserdeail();//获取客户基本信息
    this.getStudyRecordData();//获取学习记录
    this.getVIPRecordData();//获取vip记录
    this.getAnalysisDetail();//获取重点客户分析
    this.getRecord();
  },
    //获取学习记录
    getStudyRecordModel:new BaseModel({
        url:"employees/ActivityCount",
        method:"get",
        needAjaxLoading:true,
        loadingText:"玩命加载中"
    }),
    getStudyRecordData(){
        this.getStudyRecordModel
            .fetch({
                user_id: this.data.id, //用户id
            })
            .then(res => {
                this.setData({
                    studyRecord:res.data
                })
            })
            .catch(res => {
            })
    },
    //获取vip记录
    getVIPRecordModel:new BaseModel({
        url:"employees/userVip",
        method:"get",
        needAjaxLoading:true,
        loadingText:"玩命加载中"
    }),
    getVIPRecordData(){
        this.getVIPRecordModel
            .fetch({
                user_id: this.data.id, //用户id
            })
            .then(res => {
                this.setData({
                    vipRecord:res.data
                })
            })
            .catch(res => {
            })
    },
  /*历程切换事件*/
  clickNav:function(e){
    this.setData({
      tabId:e.currentTarget.dataset.id
    })
    this.getProgress();//获取个人历程
  },
  /*只有财务才有的操作*/
  clickFinancial:function(e){
    this.setData({
      financial:e.currentTarget.dataset.id
    })
    if(e.currentTarget.dataset.id==1){
      this.setData({
        financialShow:false
      })
    }else if(e.currentTarget.dataset.id==0){
      this.setData({
        financialShow:true
      })
    }
    this.getProgress();//获取个人历程
  },
  /*备注，textarea失焦事件*/
  textareablur:function(e){
    console.log(e.detail.value)
    this.setData({
      remarks:e.detail.value
    })
    this.getRemarks();
  },
  /*修改备注*/
  getRemarksModel: new BaseModel({
    url: 'updateMemo',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getRemarks(){
    this.getRemarksModel
        .fetch({
          user_id: this.data.id, //id
          memo:this.data.remarks, //备注
        })
        .then(res => {

        })
        .catch(res => {
          //失败
        })
  },
  //音频播放
 /* audioPlay: function (e) {
    var that = this,
        id = e.currentTarget.dataset.id,
        key = e.currentTarget.dataset.key,
        audioArr = that.data.audioArr,
        vidSrc = audioArr[key].src;
    myaudio.src = vidSrc;
    myaudio.autoplay = true;

    //切换显示状态
    for (var i = 0; i < audioArr.length; i++) {
      audioArr[i].bl = false;
    }
    audioArr[key].bl = true;

    //开始监听
    myaudio.onPlay(() => {
      that.setData({
        audioArr: audioArr
      })
    })

    //结束监听
    myaudio.onEnded(() => {
      audioArr[key].bl = false;
      that.setData({
        audioArr: audioArr,
      })
    })

  },
  // 音频停止
  audioStop: function (e) {
    var that = this,
        key = e.currentTarget.dataset.key,
        audioArr = that.data.audioArr;
    //切换显示状态
    for (var i = 0; i < audioArr.length; i++) {
      audioArr[i].bl = false;
    }
    audioArr[key].bl = false;

    myaudio.stop();
    //停止监听
    myaudio.onStop(() => {
      audioArr[key].bl = false;
      that.setData({
        audioArr: audioArr,
      })
    })
    //结束监听
    myaudio.onEnded(() => {
      audioArr[key].bl = false;
      that.setData({
        audioArr: audioArr,
      })
    })
  },*/
  /*查看更多*/
  look_more:function(){
    this.data.isShow=!this.data.isShow;
    this.setData({
      isShow:this.data.isShow
    })
    if(this.data.isShow==true){
      this.setData({
        look_more:"收起"
      })
    }else{
      this.setData({
        look_more:"查看更多"
      })
    }
  },
  /*切换周或月*/
    weekOrHistory:function(e){
      console.log(e.detail.value)
      if(e.detail.value){
          this.setData({
              weekOrHistory:"历史排名",
              analysis_type:2
          })
      }else{
          this.setData({
              weekOrHistory:"周排名",
              analysis_type:1
          })
      }
      this.getAnalysisDetail();
  },
  /*客户分析查看更多*/
  analysis_more:function(){
      this.data.analysis_isShow=!this.data.analysis_isShow;
      this.setData({
          analysis_isShow:this.data.analysis_isShow
      })
      if(this.data.analysis_isShow==true){
          this.setData({
              analysis_more:"收起"
          })
      }else{
          this.setData({
              analysis_more:"查看更多"
          })
      }
  },
    /*疑问*/
    analysis_doubt:function(){
      this.setData({
          isAnalysis:true
      })
  },
    /*关闭弹窗*/
    close_analysis:function(){
        var that = this;
        that.setData({
            isAnalysis:false
        })
        that.canvasRing = that.selectComponent("#A");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#B");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#C");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#D");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#E");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#F");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#G");
        that.canvasRing.showCanvasRing();
        that.canvasRing = that.selectComponent("#H");
        that.canvasRing.showCanvasRing();
    },
    /*点击分析详情*/
    analysis_detail:function(e){
        console.log(e.currentTarget.dataset.content)
        wx.showToast({
            title: e.currentTarget.dataset.content,
            icon: 'none',
            duration: 2000
        })
    },
    //获取客户基本信息
    getAnalysisDetailModel: new BaseModel({
        url: 'employees/MemberData/UserMemberData',
        method: "get",
        needAjaxLoading: true,
        loadingText: "玩命加载中"
    }),
    getAnalysisDetail() {
        var that = this;
        this.getAnalysisDetailModel
            .fetch({
                user_id: this.data.id,
                type:this.data.analysis_type
            })
            .then(res => {
                this.setData({
                    analysis_detail: res.data
                })
                that.canvasRing = that.selectComponent("#A");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#B");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#C");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#D");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#E");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#F");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#G");
                that.canvasRing.showCanvasRing();
                that.canvasRing = that.selectComponent("#H");
                that.canvasRing.showCanvasRing();

            })
            .catch(res => {
                //失败
            })
    },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var imgArr = e.currentTarget.dataset.imagelist;
    var currentImg = imgArr[e.target.dataset.img];
    wx.previewImage({
      current: currentImg, // 当前显示图片的http链接
      urls: imgArr
    })
  },
  //获取客户基本信息
  getUserdeailModel: new BaseModel({
    url: 'userDetail',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getUserdeail() {
    this.getUserdeailModel
      .fetch({
        user_id: this.data.id
      })
      .then(res => {
        this.setData({
          user_detail: res.data
        
        }),console.log(33)
      })
      .catch(res => {
        //失败
      })
  },
  /*获取个人历程记录*/
  getRecordModel: new BaseModel({
    url: 'employees/getRecord',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getRecord(){
    this.getRecordModel
        .fetch({
          user_id: this.data.id, //id
          verify:0
        })
        .then(res => {
          this.setData({
            tabId: res.data.desc,//历程id
            financial: res.data.financial,//操作
          })
          if(res.data.financial==1){
            this.setData({
              financialShow:false
            })
          }else if(res.data.financial==0){
            this.setData({
              financialShow:true
            })
          }
          this.getProgress();//获取个人历程
        })
        .catch(res => {
          //失败
        })
  },

  /*获取个人历程*/
  getProgressModel: new BaseModel({
    url: 'getProgress',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getProgress(){
    this.getProgressModel
      .fetch({
        user_id: this.data.id, //id
        desc:this.data.tabId, //历程id
        financial:this.data.financial, //操作
      })
      .then(res => {
        this.setData({
          progress: res.data
        })
      })
      .catch(res => {
        //失败
      })
  },
  /*拨打电话*/
  clickPhone:function(e){
    console.log(e.currentTarget.dataset.phone)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone // 仅为示例，并非真实的电话号码
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserdeail();//获取客户基本信息
    this.getRecord();
    this.getProgress();//获取个人历程
    this.getStudyRecordData();//获取学习记录
    this.getVIPRecordData();//获取vip记录
    this.getAnalysisDetail();//获取重点客户分析
    wx.stopPullDownRefresh(); //停止下拉
  },

})